//
//  FindPasswordViewController.swift
//  fsapp
//
//  Created by zguang on 17/8/3.
//  Copyright © 2017年 huangshengjie. All rights reserved.
//

import UIKit
import  Alamofire
class FindPasswordViewController: UIViewController{
    
    
    let RegistView = loginView(type:"regist1")
    var imageUpload = NSData()
    var contdownTime:Timer?
    
    
    //MARK: - 倒计时控制
    
    var remainSeconds: Int=0 {
        willSet{
            self.RegistView.getcodeButton.setTitle("\(newValue)秒后重新获取", for: .normal)
            self.RegistView.getcodeButton.isEnabled = false
            if newValue <= 0{
                self.RegistView.getcodeButton.setTitle("重新获取", for: .normal)
                isCounting = false
                self.RegistView.getcodeButton.isEnabled = true
                
            }
        }
    }
    
    var isCounting = false{
        willSet{
            if newValue {
                contdownTime = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
                remainSeconds = 60
                //self.viewLogin.getCode.backgroundColor = UIColor.gray
            }
            
        }
    }
    
    func updateTime(_timer:Timer){
        remainSeconds -= 1
    }
    
    
    
    
    
    //MARK: -  BASIC FUNC
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        self.navigationItem.title = "找回密码"
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.view.addSubview(RegistView.BackView)
        self.RegistView.loginNow.addTarget(self, action: #selector(next1), for: .touchUpInside)
        //RegistView.upImageButton.addTarget(self, action: #selector(pickImage), for: .touchUpInside)
        RegistView.getcodeButton.addTarget(self, action: #selector(getCode), for: .touchUpInside)
        RegistView.agreement.removeFromSuperview()
        RegistView.agreeButton.removeFromSuperview()
        RegistView.linkAgreement.removeFromSuperview()
        // Do any additional setup after loading the view.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        RegistView.codeText.resignFirstResponder()
        RegistView.passwordText.resignFirstResponder()
        RegistView.phoneText.resignFirstResponder()
        RegistView.userText.resignFirstResponder()
        RegistView.confirmPasswordText.resignFirstResponder()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func next1(){
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"",style:.plain,target:nil,action:nil)
        self.navigationController?.pushViewController(_2FindPasswordViewController(self.RegistView.codeText.text!,self.RegistView.phoneText.text!), animated: true)
        
    }
    
        
    
    func getCode(){
        let  phone = self.RegistView.phoneText.text!
        if phone == ""{
            let alert = UIAlertController(title: "提示", message: "手机号不得为空", preferredStyle: .alert)
            let action = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alert.addAction(action)
            self.present(alert, animated: true, completion: nil)
            
        }else{
            
            let url = "http://dahan-api.heiyou.net/index.php/home/user/getCode"
            let phone  = self.RegistView.phoneText.text!
            let paramDic = ["phoneNumber":phone]
            
            Alamofire.request( url, method: .post, parameters: paramDic).responseJSON { response in
                print("result=========================")
                print(response.request!)  // original URL request
                print(response.response!) // HTTP URL response
                print(response.data!)     // server data
                print(response.result)   // result of response serialization
                self.isCounting = true
                if let JSON2 = response.result.value {
                    var json = JSON(JSON2)
                    print(json)
                    //self.isCounting = true
                    if  json["status"].int != 200{
                        if  json["errCode"] == "E0002"{
                            // DispatchQueue.main.async {
                            let alert = UIAlertView(title: "号码格式不正确", message: "请稍后重试", delegate: nil, cancelButtonTitle: "知道了")
                            alert.show()
                            //  }
                        }
                        if  json["errCode"] == "A0001"{
                            // DispatchQueue.main.async {
                            let alert = UIAlertView(title: "今日验证码已达上限", message: "请稍后重试", delegate: nil, cancelButtonTitle: "知道了")
                            alert.show()
                            //  }
                        }
                        
                    }
                    else{
                        
                    }
                }
            }
            
        }//endof else
        
    }
    
    
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}



